home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / liboctave / tempname.c < prev   
C/C++ Source or Header  |  1996-10-30  |  6KB  |  212 lines

  1. /* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public
  15. License along with the GNU C Library; see the file COPYING.  If
  16. not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. Boston, MA  02111-1307, USA.  */
  18.  
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22.  
  23. #ifndef HAVE_TEMPNAM
  24.  
  25. #include <errno.h>
  26. #include <stddef.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30.  
  31. #ifdef HAVE_UNISTD_H
  32. #ifdef HAVE_SYS_TYPES_H
  33. #include <sys/types.h>
  34. #endif
  35. #include <unistd.h>
  36. #endif
  37.  
  38. #include <fcntl.h>
  39.  
  40. #include "statdefs.h"
  41.  
  42. #ifndef FILENAME_MAX
  43. #ifdef MAXPATHLEN
  44. #define FILENAME_MAX MAXPATHLEN
  45. #else
  46. #define FILENAME_MAX 1024
  47. #endif
  48. #endif
  49.  
  50. #ifndef P_tmpdir
  51. #define P_tmpdir "/usr/tmp/"
  52. #endif
  53.  
  54. /* Return nonzero if DIR is an existent directory.  */
  55. static int
  56. diraccess (const char *dir)
  57. {
  58.   struct stat buf;
  59.   return stat (dir, &buf) == 0 && S_ISDIR (buf.st_mode);
  60. }
  61.  
  62. /* Return nonzero if FILE exists.  */
  63. static int
  64. exists (const char *file)
  65. {
  66.   /* We can stat the file even if we can't read its data.  */
  67.   struct stat st;
  68.   int save = errno;
  69.   if (stat (file, &st) == 0)
  70.     return 1;
  71.   else
  72.     {
  73.       /* We report that the file exists if stat failed for a reason other
  74.      than nonexistence.  In this case, it may or may not exist, and we
  75.      don't know; but reporting that it does exist will never cause any
  76.      trouble, while reporting that it doesn't exist when it does would
  77.      violate the interface of __stdio_gen_tempname.  */
  78.       int exists = errno != ENOENT;
  79.       errno = save;
  80.       return exists;
  81.     }
  82. }
  83.  
  84.  
  85. /* These are the characters used in temporary filenames.  */
  86. static const char letters[] =
  87.   "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  88.  
  89. /* Generate a temporary filename and return it (in a static buffer).  If
  90.    STREAMPTR is not NULL, open a stream "w+b" on the file and set
  91.    *STREAMPTR to it.  If DIR_SEARCH is nonzero, DIR and PFX are used as
  92.    described for tempnam.  If not, a temporary filename in P_tmpdir with no
  93.    special prefix is generated.  If LENPTR is not NULL, *LENPTR is set the
  94.    to length (including the terminating '\0') of the resultant filename,
  95.    which is returned.  This goes through a cyclic pattern of all possible
  96.    filenames consisting of five decimal digits of the current pid and three
  97.    of the characters in `letters'.  Data for tempnam and tmpnam is kept
  98.    separate, but when tempnam is using P_tmpdir and no prefix (i.e, it is
  99.    identical to tmpnam), the same data is used.  Each potential filename is
  100.    tested for an already-existing file of the same name, and no name of an
  101.    existing file will be returned.  When the cycle reaches its end
  102.    (12345ZZZ), NULL is returned.  */
  103. char *
  104. __stdio_gen_tempname (const char *dir, const char *pfx,
  105.               int dir_search, size_t *lenptr,
  106.               FILE **streamptr)
  107. {
  108.   int saverrno = errno;
  109.   static const char tmpdir[] = P_tmpdir;
  110.   static size_t indices[2];
  111.   size_t *idx;
  112.   static char buf[FILENAME_MAX];
  113.   static pid_t oldpid = (pid_t) 0;
  114.   pid_t pid = getpid();
  115.   register size_t len, plen, dlen;
  116.  
  117.   if (dir_search)
  118.     {
  119.       register const char *d = getenv("TMPDIR");
  120.       if (d != NULL && !diraccess(d))
  121.     d = NULL;
  122.       if (d == NULL && dir != NULL && diraccess(dir))
  123.     d = dir;
  124.       if (d == NULL && diraccess(tmpdir))
  125.     d = tmpdir;
  126.       if (d == NULL && diraccess("/tmp"))
  127.     d = "/tmp";
  128.       if (d == NULL)
  129.     {
  130.       errno = ENOENT;
  131.       return NULL;
  132.     }
  133.       dir = d;
  134.     }
  135.   else
  136.     dir = tmpdir;
  137.  
  138.   dlen = strlen (dir);
  139.  
  140.   /* Remove trailing slashes from the directory name.  */
  141.   while (dlen > 1 && dir[dlen - 1] == '/')
  142.     --dlen;
  143.  
  144.   if (pfx != NULL && *pfx != '\0')
  145.     {
  146.       plen = strlen(pfx);
  147.       if (plen > 5)
  148.     plen = 5;
  149.     }
  150.   else
  151.     plen = 0;
  152.  
  153.   if (dir != tmpdir && !strcmp(dir, tmpdir))
  154.     dir = tmpdir;
  155.   idx = &indices[(plen == 0 && dir == tmpdir) ? 1 : 0];
  156.  
  157.   if (pid != oldpid)
  158.     {
  159.       oldpid = pid;
  160.       indices[0] = indices[1] = 0;
  161.     }
  162.  
  163.   len = dlen + 1 + plen + 5 + 3;
  164.   for (; *idx < ((sizeof (letters) - 1) * (sizeof (letters) - 1) *
  165.          (sizeof (letters) - 1));
  166.        ++*idx)
  167.     {
  168.       /* Construct a file name and see if it already exists.
  169.  
  170.      We use a single counter in *IDX to cycle each of three
  171.      character positions through each of 62 possible letters.  */
  172.  
  173.       if (sizeof (buf) < len)
  174.     return NULL;
  175.  
  176.       sprintf (buf, "%.*s/%.*s%.5d%c%c%c",
  177.            (int) dlen, dir, (int) plen,
  178.            pfx, pid % 100000,
  179.            letters[*idx
  180.                % (sizeof (letters) - 1)],
  181.            letters[(*idx / (sizeof (letters) - 1))
  182.                % (sizeof (letters) - 1)],
  183.            letters[(*idx / ((sizeof (letters) - 1) *
  184.                 (sizeof (letters) - 1)))
  185.                % (sizeof (letters) - 1)]
  186.            );
  187.  
  188.       if (! buf || strlen (buf) != (int) len)
  189.     return NULL;
  190.   
  191.       if (streamptr != NULL)
  192.     abort ();
  193.       else if (exists (buf))
  194.     continue;
  195.  
  196.       /* If the file already existed we have continued the loop above,
  197.      so we only get here when we have a winning name to return.  */
  198.  
  199.       errno = saverrno;
  200.  
  201.       if (lenptr != NULL)
  202.     *lenptr = len + 1;
  203.       return buf;
  204.     }
  205.  
  206.   /* We got out of the loop because we ran out of combinations to try.  */
  207.   errno = EEXIST;        /* ? */
  208.   return NULL;
  209. }
  210.  
  211. #endif
  212.